package com.example.administrator.szb.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseFragmentActivity;
import com.example.administrator.szb.fragments.askmannager.AskManagerFragment;

public class AskMannagerActivity extends MVPBaseFragmentActivity implements View.OnClickListener{
    private LinearLayout activity_guan_zhu,tab_ll;
    private TextView bar_right,bar_title;
    private TextView tv_1;
    private TextView tv_2;
    private TextView tv_3;
    public static boolean canShowCheckBox = false;
    private AskManagerFragment fragment1,fragment2,fragment3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ask_mannager);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        tab_ll = findViewById(R.id.tab_ll);
        activity_guan_zhu = (LinearLayout) findViewById(R.id.activity_guan_zhu);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv_3 = (TextView) findViewById(R.id.tv_3);
        bar_title = findViewById(R.id.bar_title);
        bar_right = findViewById(R.id.bar_right);
        bar_title.setText("问答管理");
        bar_right.setText("编辑");
        bar_right.setVisibility(View.VISIBLE);
        bar_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (canShowCheckBox){
                    canShowCheckBox = false;
                    bar_right.setText("编辑");
                    //通知Fragment隐藏CheckBox
                }else {
                    canShowCheckBox = true;
                    bar_right.setText("取消");
                    //通知Fragment显示CheckBox
                }
                notifyFrgament();
            }
        });
    }
    private boolean isPuTongUser = false;
    private int utype = -1;//1我的问题   3我的回答
    private void initFrom(){
        utype = getIntent().getIntExtra("type",-1);
        if (utype!=-1){
            isPuTongUser = true;
            if (utype==1){

                bar_title.setText("我的问题");
            }else {

                bar_title.setText("我的回答");
            }
            tab_ll.setVisibility(View.GONE);
            bar_right.setVisibility(View.GONE);
        }
    }
    @Override
    protected void initEvent() {
        tv_1.setOnClickListener(this);
        tv_2.setOnClickListener(this);
        tv_3.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        initFrom();
        canShowCheckBox = false;
        if (isPuTongUser){
            //是普通用户的话，就使用frgment3,不管我的问题还是我的回答，具体在fragment里解释
            onClick(tv_3);
        }else {
            onClick(tv_1);
        }

    }

    @Override
    public void onClick(View v) {
        if (bar_right.getText().toString().equals("取消")){
            return;
        }
        FragmentTransaction transaction = getFragmentTransaction();
        reset();
        hideFragment(transaction);

        switch (v.getId()){
            case R.id.tv_1:
                tv_1.setTextColor(getResources().getColor(R.color.basecolor));
                if (fragment1==null){
                    fragment1 = new AskManagerFragment();
                    fragment1.setType(1);
                    transaction.add(R.id.frameLayout,fragment1);
                }else {
                    transaction.show(fragment1);
                }
                break;
            case R.id.tv_2:

                tv_2.setTextColor(getResources().getColor(R.color.basecolor));
                if (fragment2==null){
                    fragment2 = new AskManagerFragment();
                    fragment2.setType(2);
                    transaction.add(R.id.frameLayout,fragment2);
                }else {
                    transaction.show(fragment2);
                }
                break;
            case R.id.tv_3:
                tv_3.setTextColor(getResources().getColor(R.color.basecolor));
                if (fragment3==null){
                    fragment3 = new AskManagerFragment();
                    fragment3.setType(3);
                    if (isPuTongUser&&utype==1){
                        fragment3.setFromUserDatas(true);
                    }
                    transaction.add(R.id.frameLayout,fragment3);
                }else {
                    transaction.show(fragment3);
                }

                break;
        }

        transaction.commitAllowingStateLoss();
    }

    public void notifyFrgament(){
        if (!canShowCheckBox){
            bar_right.setText("编辑");
        }
        if (fragment1!=null){
            fragment1.notifys();
        }
        if (fragment2!=null){
            fragment2.notifys();
        }
        if (fragment3!=null){
            fragment3.notifys();
        }
    }

    private void hideFragment(FragmentTransaction transaction){
        if (fragment1!=null){
            transaction.hide(fragment1);
        }
        if (fragment2!=null){
            transaction.hide(fragment2);
        }
        if (fragment3!=null){
            transaction.hide(fragment3);
        }
    }

    public void reset(){
        tv_1.setTextColor(getResources().getColor(R.color.gray1));
        tv_2.setTextColor(getResources().getColor(R.color.gray1));
        tv_3.setTextColor(getResources().getColor(R.color.gray1));
    }
}
